App({
  onLaunch() {
    console.log("App launched");
    // 检查用户是否已经注册
    const userInfo = wx.getStorageSync("userInfo");
    const token = wx.getStorageSync("token");
    if (userInfo && token) {
      this.globalData.userInfo = userInfo;
      this.globalData.token = token;
      console.log("用户已登录: userInfo", userInfo);
      console.log("用户已登录: token", token);
    } else {
      // 如果未登录，则跳转到登录页面
      wx.redirectTo({ url: "/pages/login/login" });
    }
  },
  globalData: {
    userInfo: null,   // 全局存储用户信息
    token: null, // 全局存储token
    theme: 'light', // 默认主题
    themes: {
      light: {
        class: 'light',
        navBarColor: '#ffffff',
        navBarText: '#000000',
        gradient: 'linear-gradient(90deg, #ff416c, #ff4b2b)'
      },
      dark: {
        class: 'dark',
        navBarColor: '#2d2d2d',
        navBarText: '#ffffff',
        gradient: 'linear-gradient(90deg, #4a4a4a, #2d2d2d)'
      }
    }
  },

  // 全局主题切换方法
  setTheme(theme) {
    if (!this.globalData.themes[theme]) return
    this.globalData.theme = theme
    this.applyGlobalTheme()
  },

  // 应用主题到全局
  applyGlobalTheme() {
    const theme = this.globalData.themes[this.globalData.theme]
    // 更新所有页面
    const pages = getCurrentPages()
    pages.forEach(page => {
      page.setData({
        themeClass: theme.class,
        themeGradient: theme.gradient
      })
    })
    // 更新导航栏
    wx.setNavigationBarColor({
      frontColor: theme.navBarText,
      backgroundColor: theme.navBarColor
    })
  }
});
